package cn.yunyichina.provider.pay.alipay.service.impl;

import cn.yunyichina.provider.iface.entity.alipay.AddAlipayCouponResponse;
import cn.yunyichina.provider.iface.entity.alipay.AlipayRequest;
import cn.yunyichina.provider.pay.alipay.config.Alipay;
import cn.yunyichina.provider.pay.alipay.entity.AlipassInstanceAddResponse;
import cn.yunyichina.provider.pay.alipay.service.AlipassService;
import cn.yunyichina.provider.pay.alipay.service.BaseService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayPassInstanceAddRequest;
import com.alipay.api.response.AlipayPassInstanceAddResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2017/3/27.
 */
@Service
public class AlipassServiceImpl extends BaseService implements AlipassService {

    private static final Logger logger = LoggerFactory.getLogger(AlipassServiceImpl.class);

    public AddAlipayCouponResponse addAlipassInstance(AlipayRequest alipayRequest) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                getConfig(Alipay.ALIPAY_GATEWAY_OPENAPI),
                alipayRequest.getAppId(),
                alipayRequest.getPrivateKey(),
                alipayRequest.getFormat(),
                alipayRequest.getCharset(),
                alipayRequest.getPublicKey(),
                alipayRequest.getSignType()
        );

        AlipayPassInstanceAddRequest request = new AlipayPassInstanceAddRequest();
        String bizContent = alipayRequest.getBizContent();
        logger.info("AlipassServiceImpl.addAlipassInstance.bizContent -> {}", bizContent);
        request.setBizContent(bizContent);
        try {
            AlipayPassInstanceAddResponse addResponse = alipayClient.execute(request);
            logger.info("AlipassServiceImpl.addAlipassInstance.addResponse -> {}", JsonUtils.toJsonString(addResponse));
            if (addResponse.isSuccess()) {
                String body = addResponse.getBody();
                logger.info("AlipassServiceImpl.addAlipassInstance.body -> {}", body);

                AlipassInstanceAddResponse alipassInstanceAddResponse = JsonUtils.parseObject(body, AlipassInstanceAddResponse.class);
                logger.info("AlipassServiceImpl.addAlipassInstance.alipassInstanceAddResponse -> {}", JsonUtils.toJsonString(alipassInstanceAddResponse));

                AlipassInstanceAddResponse.AlipayPassInstanceAddResponse alipayPassInstanceAddResponse = alipassInstanceAddResponse.getAlipayPassInstanceAddResponse();
                String content = JsonUtils.toJsonString(alipayPassInstanceAddResponse);
                logger.info("AlipassServiceImpl.addAlipassInstance.content -> {}", content);

                if (alipassInstanceAddResponse.getAlipayPassInstanceAddResponse().isSuccess()) {
                    AddAlipayCouponResponse response = new AddAlipayCouponResponse();
                    AlipassInstanceAddResponse.AlipayPassInstanceAddResponse.Result result = JSON.parseObject(alipayPassInstanceAddResponse.getResult(), AlipassInstanceAddResponse.AlipayPassInstanceAddResponse.Result.class);
                    response.setOutCouponId(result.getPassId());
                    response.setCouponId(result.getSerialNumber());

                    return response;
                }
            }
        } catch (AlipayApiException e) {
            logger.error("AlipassServiceImpl.addAlipassInstance.AlipayApiException", e);
        }

        return null;
    }

}
